package com.example.redstarcommerce.cn.dh.coupon.controller;

import com.example.redstarcommerce.cn.dh.common.vo.Result;
import com.example.redstarcommerce.cn.dh.coupon.pojo.CouPon;
import com.example.redstarcommerce.cn.dh.coupon.service.impl.CouPonServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * @ClassName CouPonController
 * @Author AndyWu
 * @Date 2024-11-01 22:33
 * @Motto 学不死就往死里学
 * @Version 1.0
 */
@RestController
@RequestMapping("/coupon")
//类接口
@Api(tags = "优惠卷接口",description = "优惠卷")
@Slf4j
public class CouPonController {


    @Autowired
    private CouPonServiceImpl couPonService;


    // 方法接口
    @RequestMapping("/insertcoupon")
    @ApiOperation(value = "添加优惠卷", notes = "insertcoupon", httpMethod = "POST")
    public Result<Boolean> insertCouPon(String name, String type, Float price, Integer num, String begintime, String endtime) {
        log.info("insertCouPon: {}, {}, {}, {}, {}, {}", name, type, price, num, begintime, endtime);

        // 定义输入时间格式
        SimpleDateFormat inputSdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT'Z (zzzz)", Locale.CHINA);

        Timestamp begintime1 = null;
        Timestamp endtime1 = null;

        try {
            // 解析输入时间字符串
            Date beginDate = inputSdf.parse(begintime);
            Date endDate = inputSdf.parse(endtime);
            // 转换为 Timestamp
            begintime1 = new Timestamp(beginDate.getTime());
            endtime1 = new Timestamp(endDate.getTime());
        } catch (ParseException e) {
            // 直接抛出异常或记录日志，但不返回错误信息给客户端
            log.error("时间格式不正确，请使用 EEE MMM dd yyyy HH:mm:ss 'GMT'Z (zzzz) 格式", e);
            // 强制转换失败时，可以设置默认值或抛出自定义异常
            begintime1 = new Timestamp(System.currentTimeMillis()); // 设置默认值
            endtime1 = new Timestamp(System.currentTimeMillis()); // 设置默认值
        }

        Result<Boolean> booleanResult = couPonService.insertCouPon(name, type, price, num, begintime1, endtime1);
        log.info("insertCouPon result: {}", booleanResult);
        return booleanResult;
    }



    @RequestMapping("/getallcoupon")
    @ApiOperation(value = "查询所有优惠卷", notes = "getAllCouPon", httpMethod = "GET")
    public Object getAllCouPon() {
        Result<List<CouPon>> all = couPonService.getAllCouPon();
        return all;
    }


    @RequestMapping("/getbycoupon")
    @ApiOperation(value ="根据条件查询优惠卷",notes = "getbycoupon",httpMethod = "GET")
    public Object getByCouPon(String name, String type, Float price, Integer num, Timestamp begintime, Timestamp endtime) {

        // 调用服务层方法，根据传入的条件获取优惠券列表
        Result<List<CouPon>> listResult = couPonService.getByCouPon(name, type, price, num, begintime, endtime);
        // 记录查询结果日志
        log.info("getByCouPon"+listResult);
        // 返回查询结果
        return listResult;
    }

    @RequestMapping("/updatecoupon")
    @ApiOperation(value ="修改优惠卷",notes = "updatecoupon",httpMethod = "POST")
    public Result<Boolean> updateCouPon(Integer id, String name, String type, Float price, Integer num, String begintime, String endtime) {

        log.info("insertCouPon: {}, {}, {}, {}, {}, {}", name, type, price, num, begintime, endtime);

        // 定义输入时间格式
        SimpleDateFormat inputSdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT'Z (zzzz)", Locale.CHINA);

        Timestamp begintime1 = null;
        Timestamp endtime1 = null;

        try {
            // 解析输入时间字符串
            Date beginDate = inputSdf.parse(begintime);
            Date endDate = inputSdf.parse(endtime);
            // 转换为 Timestamp
            begintime1 = new Timestamp(beginDate.getTime());
            endtime1 = new Timestamp(endDate.getTime());
        } catch (ParseException e) {
            // 直接抛出异常或记录日志，但不返回错误信息给客户端
            log.error("时间格式不正确，请使用 EEE MMM dd yyyy HH:mm:ss 'GMT'Z (zzzz) 格式", e);
            // 强制转换失败时，可以设置默认值或抛出自定义异常
            begintime1 = new Timestamp(System.currentTimeMillis()); // 设置默认值
            endtime1 = new Timestamp(System.currentTimeMillis()); // 设置默认值
        }

        Result<Boolean> booleanResult = couPonService.updateCouPon(id, name, type, price, num, begintime1, endtime1);
        log.info("updateCouPon"+booleanResult);
        return booleanResult;
    }

    @RequestMapping("/deletecoupon")
    @ApiOperation(value ="删除优惠卷",notes = "deletecoupon",httpMethod = "POST")
    public Result<Boolean> deleteCouPon(Integer id) {
        Result<Boolean> booleanResult = couPonService.deleteCouPon(id);
        log.info("deleteCouPon"+booleanResult);
        return booleanResult;
    }


}
